package com.dfdchem.imd.kaiwu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.model.AlarmConfigModel;
import com.dfdchem.imd.kaiwu.model.AlarmHandler;
import com.dfdchem.imd.kaiwu.model.AlarmModel;
import com.dfdchem.imd.kaiwu.util.AlarmUtil;

import java.util.List;

public class AlarmListAdapter extends RecyclerView.Adapter<AlarmListAdapter.ViewHolder> {

    private final static String TAG = "AlarmListAdapter";
    private List<AlarmModel> alarmModels;

    private Context context;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public AlarmListAdapter(List<AlarmModel> alarmModels) {
        this.alarmModels = alarmModels;
    }

    public void setAlarmModels(List<AlarmModel> alarmModels) {
        this.alarmModels = alarmModels;
    }
    public List<AlarmModel> getAlarmModels() {
        return alarmModels;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView no;
        public TextView name;
        public TextView exp;
        public TextView minValue;
        public TextView maxValue;
        public TextView handler_1;
        public TextView handler_2;
        public TextView handler_3;
        public TextView start_time;
        public TextView end_time;
        public TextView alarm_level_tips;
        public ViewHolder(View view) {
            super(view);
            no = view.findViewById(R.id.no);
            name = view.findViewById(R.id.name);
            exp = view.findViewById(R.id.exp);
            minValue = view.findViewById(R.id.minValue);
            maxValue = view.findViewById(R.id.maxValue);
            handler_1 = view.findViewById(R.id.handler_1);
            handler_2 = view.findViewById(R.id.handler_2);
            handler_3 = view.findViewById(R.id.handler_3);
            start_time = view.findViewById(R.id.start_time);
            end_time = view.findViewById(R.id.end_time);
            alarm_level_tips = view.findViewById(R.id.alarm_level_tips);
        }
    }

    @Override
    public AlarmListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                          int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.alarm_list_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AlarmModel am = alarmModels.get(position);
        AlarmConfigModel acm = am.getConfig();
        holder.no.setText(am.getNo());
        holder.name.setText(am.getName());

        holder.exp.setText(AlarmUtil.getTextByExp(acm.getExp()));
        if (acm.getExp() == 14 || acm.getExp() == 15) {
            //TODO大于和大于等于 需要取ValueMin，后续有时间才调整吧
            holder.minValue.setText("-");
            holder.maxValue.setText(acm.getValueMin() != null ? acm.getValueMin().toString() : "-");
        } else {
            holder.minValue.setText(acm.getValueMin() != null ? acm.getValueMin().toString() : "-");
            holder.maxValue.setText(acm.getValueMax() != null ? acm.getValueMax().toString() : "-");
        }

        List<AlarmHandler> list = am.getHandlerList();
        if (list != null) {
            if (list.size() > 0) {
                holder.handler_1.setText(list.get(0).getUserName());
            }

            if (list.size() > 1) {
                holder.handler_2.setText(list.get(1).getUserName());
            }

            if (list.size() > 2) {
                holder.handler_3.setText(list.get(2).getUserName());
            }
        }

        holder.start_time.setText(am.getStartTime());
        holder.end_time.setText(am.getEndTime());
        holder.alarm_level_tips.setText(AlarmUtil.getTextByAlarmLevel(am.getAlarmLevel()));
        holder.alarm_level_tips.setBackgroundColor(context.getResources().getColor(AlarmUtil.getColorByAlarmLevel(am.getAlarmLevel())));
    }

    @Override
    public int getItemCount() {
        return alarmModels == null ? 0 : alarmModels.size();
    }

}
